package graph.editor;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.RectangularShape;
import java.io.Serializable;

public class Vertex implements Serializable {
	RectangularShape shape;
	String label;
	Color vColor;
	boolean initi = false;
	boolean fin = false;
	private static final Point2D DELTA_LABEL = new Point(1, -1);

	Vertex(RectangularShape rs, String label, Color color,boolean initi, boolean fin) {
		this.shape = rs;
		this.label = label;
		this.vColor = color;
		this.initi = initi;
		this.fin=fin ;
	}

	public boolean contains(int x, int y) {
		return shape.contains(x, y);
	}

	public boolean getFinstate() {
		return fin;
	}
	public void setFinstate(boolean fin)
	{
		this.fin=fin;
	}
	public boolean getInistate()
	{
		return initi;
	}

	public void draw(Graphics2D g2) {
		g2.setColor(vColor);
		g2.fill(shape);
	
		if (label != null)
			g2.drawString(label, (int) (shape.getMaxX() + DELTA_LABEL.getX()),
					(int) (shape.getMinY() + DELTA_LABEL.getY()));

		if (fin == true) {
			Graphics2D g = (Graphics2D) g2.create();

			// double dx = x2 - x1, dy = y2 - y1;
			double angle = Math.atan2(shape.getCenterX(), shape.getCenterY());
			int len = (int) Math.sqrt(1000);
			AffineTransform at = AffineTransform.getTranslateInstance(
					shape.getCenterX() + 22, shape.getCenterY());
			// at.concatenate(AffineTransform.getRotateInstance(angle));
			g.transform(at);

			// Draw horizontal arrow starting in (0, 0)
			g.drawLine(len, 0, len - 30, 0);
			g.fillPolygon(new int[] { len, len - 4, len - 4, len }, new int[] {
					1, -4, 4, 0 }, 4);
		}
	}

	public void setColor(Color color) {
		vColor = color;
	}
}
